
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Login extends JFrame{

    public Login(){

        SQLOperation sql=new SQLOperation();

        setTitle("Login");
        Container container=getContentPane();
        setLayout(new BorderLayout());

        JLabel title=new JLabel("Please choose your identity and log in the system.");
        title.setFont(new Font("",Font.BOLD,20));
        title.setHorizontalAlignment(SwingConstants.CENTER);

        add(title,BorderLayout.NORTH);


        JPanel account=new JPanel(new GridLayout(3,1,5,5));

        JPanel type=new JPanel();

        type.add(new JLabel("Choose your identity:"));
        JComboBox enterType=new JComboBox();
        enterType.addItem("--------------------");
        enterType.addItem("System manager");
        enterType.addItem("Equipment factory");
        type.add(enterType);
        account.add(type);
        //enterType.getSelectedIndex();
        //0--unchoose,1--System manager,2--Equipment factory


        JPanel ID=new JPanel();

        ID.add(new JLabel("         UID:"));
        JTextField enterID=new JTextField(18);
        ID.add(enterID);
        account.add(ID);
        //enterID.getText();


        JPanel password=new JPanel();

        password.add(new JLabel("Password:"));
        JPasswordField enterPassword=new JPasswordField(18);

        enterPassword.setEchoChar('*');
        password.add(enterPassword);
        account.add(password);
        //enterPassword.getPassword();

        add(account,BorderLayout.CENTER);

        JPanel buttons=new JPanel();

        JButton exit=new JButton("Exit");

        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int exitOperation=JOptionPane.showConfirmDialog
                        (null, "Confirm exit?","Confirm",JOptionPane.YES_NO_OPTION);

                if(exitOperation==JOptionPane.YES_OPTION){
                    System.exit(1);
                }

            }
        });

        buttons.add(exit);

/*
        JButton register=new JButton("Register");

        Object[] identity= {"System manager","Equipment factory"};
        register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int registerOperation = JOptionPane.showOptionDialog
                        (null, "Choose your identity","Register identity",JOptionPane.YES_NO_CANCEL_OPTION,
                                JOptionPane.QUESTION_MESSAGE,null, identity, identity[0]);
                if(registerOperation==0){
                    //Go system manager register frame


                }
                else if(registerOperation==1){
                    //Go equipment factory register frame


                }

            }
        });

        buttons.add(register);

*/
        JButton log=new JButton("Login");



        log.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] attri={"UID","Password"};
                Object[][] accountInfo=sql.search("Account",attri,"shanchu",1);


          //          System.out.println(accountInfo[0][0]);

                if(enterID.getText().equals("")||enterPassword.getText().equals("")||enterType.getSelectedIndex()==0){
                    JOptionPane.showMessageDialog
                            (null, "Empty message!","Empty",JOptionPane.ERROR_MESSAGE);
                    return;
                }
                boolean isContained=false;
                int position=-1;
                for(int i=0;i<accountInfo.length;i++){
                    if(accountInfo[i][0].equals(enterID.getText())){
                        isContained=true;
                        position=i;
                        break;
                    }
                }

                if(!isContained){
                    //unfound username

                    JOptionPane.showMessageDialog
                            (null, "Invalid username!","Invalid",JOptionPane.ERROR_MESSAGE);
                    enterID.setText("");enterPassword.setText("");

                    return;

                }
                else if(!accountInfo[position][1].equals(enterPassword.getText())) {
                    //wrong password

                    JOptionPane.showMessageDialog
                            (null, "Wrong password!", "Wrong", JOptionPane.ERROR_MESSAGE);
                    enterPassword.setText("");
                    return;
                }
                else{
                    setVisible(false);
                    String id=String.valueOf(accountInfo[position][0]);

                    if(enterType.getSelectedIndex()==1) {
                        new ManagerDefaultFrame(id);
                    }

                    else if(enterType.getSelectedIndex()==2){
                        new FactoryDefaultFrame(id);
                    }
                    //successfully login

                }
            }
        });

        buttons.add(log);

        add(buttons,BorderLayout.SOUTH);

        setSize(500,225);
        setResizable(false);
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
        setVisible(true);
    }



}
